<?php

namespace app\frontend\controller;

use app\common\model\Avatar;
use app\common\model\BgPosts;
use app\common\model\Fans;
use app\common\model\Follows;
use app\common\model\Users;
use think\Controller;
use think\Request;

class User extends Controller
{
    //前置操作
    protected $beforeActionList=[
        'first',
    ];

    public function first(){
        if(!session('uid') || !session('name')) {
            return $this->redirect('/login');
        }
    }

    public function index(Request $request,$id)
    {
        //TODO 此功能没有测试取消关注和再关注 status0 ,1

        //用户信息
        $user_info=Users::where('status',1)->where('uid',$id)->find();
        if(!$user_info){
            return renderJs('非法操作','/postss');
        }

        //用户头像
        $avatar=Avatar::where('user_id',$id)->value('img_url');


        //用户所属文章信息
        $posts=BgPosts::where('uid',$id)->where('status',1)->order('id','desc')->paginate(5);

        //用户关注信息
        $follows=Follows::where('status',1)->where('user_id',$id)->paginate(5);
        $follows_count=count($follows);

        //用户粉丝信息
        $fans=Fans::where('status',1)->where('user_id',$id)->paginate(5);
        $fans_count=count($fans);

        #[计数]
        //用户所发布文章计数
        $posts_count=count($posts);
        $counts=[
            'posts_count'=>$posts_count,
            'follows_count'=>$follows_count,
            'fans_count'=>$fans_count
        ];
        $this->assign(compact('user_info','posts','counts','follows','fans','avatar'));
        return $this->fetch();
    }

    //关注[Ajax处理]
    public function fan(Request $request,$id){
//        return ['msg'=>$id,'error'=>0];
        $has_user=Users::where('status',1)->where('uid',$id)->find();
        if(!$has_user){
            return ['msg'=>'非法操作','error'=>-1];
        }

        //验证
        if(!$id || session('uid')==$id){
            return ['msg'=>'非法操作','error'=>-1];
        }

        //写入数据库
        ///写入follows
        $follow_info=Follows::where('user_id',session('uid'))->where('followed_user',$id)->where('status',0)->find();
        if(!$follow_info){
            $follow_info=new Follows;
            $follow_info->user_id=session('uid');
            $follow_info->followed_user=$id;
        }else{
            $follow_info->status=1;
        }

        $follow_info->save();

        ///写入fans
        $fan_info=Fans::where('user_id',$id)->where('fan_id',session('uid'))->where('status',0)->find();
        if(!$fan_info){
            $fan_info=new Fans;
            $fan_info->user_id=$id;
            $fan_info->fan_id=session('uid');
        }else{
            $fan_info->status=1;
        }
        $fan_info->save();

        return ['msg'=>"关注{$has_user['nickname']}成功",'error'=>0];
    }

    //取消关注
    public function unfan(Request $request,$id){
        $has_user=Users::where('status',1)->where('uid',$id)->find();
        if(!$has_user || !$id){
            return ['msg'=>'非法操作','error'=>-1];
        }

        $follow_info=Follows::where('user_id',session('uid'))->where('followed_user',$id)->where('status',1)->find();

        $fan_info=Fans::where('fan_id',session('uid'))->where('user_id',$id)->where('status',1)->find();
        //验证
        if(!$follow_info || !$fan_info){
            return ['msg'=>'你并未关注此人','error'=>-1];
        }

        $follow_info->status=0;
        $follow_info->save();

        $fan_info->status=0;
        $fan_info->save();

        return ['msg'=>'成功','error'=>0];
    }

    //个人设置
    public function setting(Request $request,$id){
        if(session('uid')!==intval($id)){
            return renderJs('不是用户本人,无法操作','/');
        }
        $user_info=Users::where('uid',$id)->where('status',1)->find();
        if(!$user_info){
            return renderJs('不存在或被禁止的用户','/');
        }

        $avatar_url=Avatar::where('user_id',$id)->value('img_url');
        //显示个人信息界面
        if($request->isGet()) {
            $this->assign(compact('id','user_info','avatar_url'));
            return $this->fetch();
        }


        //dump($request->post());
        $name=trim($request->post('name'));

        //验证用户信息
        $msg_err=$this->validate($request->post(),[
            'name'=>'require|length:3,20|token'
        ],[
            'name.require'=>'用户名不能为空',
            'name.length'=>'用户名长度在3~20个字符之间'
        ]);
        if($msg_err !==true){
            return renderJs($msg_err);
        }

        //检验文件
        $file=$request->file();

        if($file) {

            $file_msg=$this->validate($file,[
                'avatar'=>'file|fileSize:2097100|fileExt:jpg,jpeg,png,gif'
            ],[
                'avatar.fileSize'=>'上传图片要小于2M',
                'avatar.fileExt'=>'仅支持jpg,jpeg,png,gif'
            ]);

            if($file_msg!==true){
                return renderJs($file_msg);
            }else{
                $upload_file=$request->file('avatar');
                $info=$upload_file->move('uploads');
                if($info){
                    $img_url=$info->getSaveName();
                }else{
                    return renderJs($upload_file->getError());
                }
            }
            //return  'ssss';
        }

        //写入数据库
        //如果用户修改了用户名[修改nickname信息]
        if($name !== $user_info['nickname']){
            //检查$name是否唯一
            $has_name=Users::where('nickname',$name)->find();
            if($has_name){
                return renderJs('此用户名已存在');
            }
            $user_info->nickname=$name;
            $user_info->save();

            session('name',$name);
        }

        //用户上传了头像图片
        if(isset($img_url)){
            //写入头像表中头像地址和user映射关系
            $avatar=Avatar::where('user_id',$user_info->uid)->find();
            if(!$avatar) {
                $avatar = new Avatar;
                $avatar->user_id = $user_info->uid;
            }
            $avatar->img_url=$img_url;
            $avatar->save();

            session('avatar_url',$img_url);
        }

        //如果用户什么也没修改而点击提交
        return renderJs('修改成功');
    }
}
